package dao;

import entity.Schedule;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ScheduleDaoImpl implements IScheduleDao {
    private static Map<String, Schedule> scheduleMap = new ConcurrentHashMap<>();

    @Override
    public boolean addSchedule(Schedule schedule) {
        if (scheduleMap.containsKey(schedule.getScheduleId())) {
            return false;
        }
        scheduleMap.put(schedule.getScheduleId(), schedule);
        return true;
    }

    @Override
    public boolean deleteSchedule(String scheduleId) {
        if (!scheduleMap.containsKey(scheduleId)) {
            return false;
        }
        scheduleMap.remove(scheduleId);
        return true;
    }

    @Override
    public boolean updateSchedule(Schedule schedule) {
        if (!scheduleMap.containsKey(schedule.getScheduleId())) {
            return false;
        }
        scheduleMap.put(schedule.getScheduleId(), schedule);
        return true;
    }

    @Override
    public Schedule getScheduleById(String scheduleId) {
        return scheduleMap.get(scheduleId);
    }

    @Override
    public List<Schedule> getAllSchedules() {
        return new ArrayList<>(scheduleMap.values());
    }

    @Override
    public List<Schedule> getSchedulesByDate(Date date) {
        List<Schedule> result = new ArrayList<>();
        for (Schedule schedule : scheduleMap.values()) {
            if (schedule.getStartTime().getDate() == date.getDate() &&
                    schedule.getStartTime().getMonth() == date.getMonth() &&
                    schedule.getStartTime().getYear() == date.getYear()) {
                result.add(schedule);
            }
        }
        return result;
    }

    @Override
    public List<Schedule> getSchedulesByMovieId(String movieId) {
        List<Schedule> result = new ArrayList<>();
        for (Schedule schedule : scheduleMap.values()) {
            if (schedule.getMovie().getMovieId().equals(movieId)) {
                result.add(schedule);
            }
        }
        return result;
    }
}